package Dao;

import DataClass.LoginAndExit;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @since 2024-06-03
 * @author yooo_fan
 * 数据库访问对象
 */
public class LoginAndExitRecordDao {

    private static PreparedStatement getLoginAndExitPreparedStatement = null;

    private static PreparedStatement getLoginAndExitWithTeacherPreparedStatement = null;

    public static List<LoginAndExit> getLoginAneExitRecord() throws SQLException {
        List<LoginAndExit> list = new ArrayList<>();
        ResultSet data;
        try {
            data = LoginAndExitRecordDao.getLogInAndExit();
        }catch (SQLException e) {
            System.err.println("登入登出数据查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()) {
            String rpName = data.getString("rp_name");
            String ruId = data.getString("RU_id");
            Integer infoId = data.getInt("Info_id");
            String Info_Type = data.getString("Info_Type");
            Timestamp Info_InTime = data.getTimestamp("Info_InTime");
            Timestamp Info_OutTime = data.getTimestamp("Info_OutTime");
            String RUTeacher = data.getString("RU_Teacher");
            String RUAnoTeacher = data.getString("RU_AnoTeacher");
            list.add(new LoginAndExit(rpName,ruId,infoId,Info_Type,Info_InTime,Info_OutTime,RUTeacher,RUAnoTeacher));
        }

        JBDCUtil.closeResultSet(data);

        return list;
    }

    /**
     * 通过教师名称获取记录
     * @param TeacherName 教师名称
     * @return datalist
     * @throws SQLException 数据库异常
     */
    public static List<LoginAndExit> getLoginAneExitRecordWithTeacherName(String TeacherName) throws SQLException {
        System.out.println("正在获取登录信息"+TeacherName);
        List<LoginAndExit> list = new ArrayList<>();
        ResultSet data;
        try {
            data = LoginAndExitRecordDao.getLogInAndExitWithTeacherName(TeacherName);
        }catch (SQLException e) {
            System.err.println("登入登出数据查询失败 from Dao");
            throw new SQLException();
        }

        while(data.next()) {
            String rpName = data.getString("rp_name");
            String ruId = data.getString("RU_id");
            Integer infoId = data.getInt("Info_id");
            String Info_Type = data.getString("Info_Type");
            Timestamp Info_InTime = data.getTimestamp("Info_InTime");
            Timestamp Info_OutTime = data.getTimestamp("Info_OutTime");
            String RUTeacher = data.getString("RU_Teacher");
            String RUAnoTeacher = data.getString("RU_AnoTeacher");
            list.add(new LoginAndExit(rpName,ruId,infoId,Info_Type,Info_InTime,Info_OutTime,RUTeacher,RUAnoTeacher));
        }
        JBDCUtil.closeResultSet(data);

        return list;
    }


    /**
     * 获取所有登录登出信息
     * @return 返回登录登出信息
     * @throws SQLException 数据库错误
     */
    public static ResultSet getLogInAndExit() throws SQLException{
        if(getLoginAndExitPreparedStatement == null || getLoginAndExitPreparedStatement.isClosed())
        {
            String sql = "select * from login_and_exit";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getLoginAndExitPreparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet result = getLoginAndExitPreparedStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }


    /**
     * 通过主讲教师名称获取记录
     * @param teacherName 教师名称
     * @return ResultSet
     * @throws SQLException 数据库异常
     */
    public static ResultSet getLogInAndExitWithTeacherName(String teacherName) throws SQLException{
        if(getLoginAndExitWithTeacherPreparedStatement == null || getLoginAndExitWithTeacherPreparedStatement.isClosed())
        {
            String sql = "select * from login_and_exit where trim(RU_Teacher) = ?";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getLoginAndExitWithTeacherPreparedStatement = JBDCUtil.getPreparedStatement(sql);
        }

        getLoginAndExitWithTeacherPreparedStatement.setString(1,teacherName);
        ResultSet result = getLoginAndExitWithTeacherPreparedStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }
}
